1552B - Running for Gold - CodeForces Solution


combinatorics graphs greedy sortings *1500

Please click on ads to support us..

Python Code:

import functools


def my_cmp(o1, o2):
    cnt = 0
    for i in range(5):
        if o1[i] < o2[i]:
            cnt += 1
    return -1 if cnt >= 3 else 1


for _ in range(int(input())):
    n = int(input())
    a = [list(map(int, input().split())) + [i + 1] for i in range(n)]
    candidate = a[0]
    for i in range(1, n):
        if my_cmp(candidate, a[i]) > 0:
            candidate = a[i]

    good = True
    for i in range(0, n):
        if a[i] != candidate and my_cmp(candidate, a[i]) > 0:
            good = False
            break

    if good:
        print(candidate[5])
    else:
        print(-1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        vector<vector<int> > a(n, vector<int>(5));
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < 5;j++)
            {
                //cout << "11111"<<endl;
                cin >> a[i][j];
            }
        }
        int id = 0;
        for(int i = 1;i < n;i++)
        {
            int sum = 0;
            for(int j = 0;j < 5;j++)
            {
                sum += a[i][j] < a[id][j];
            }
            if(sum >= 3)
            {
                id = i;
            }
        }
        for(int i = 0;i < n;i++)
        {
            int sum = 0;
            for(int j = 0;j < 5;j++)
            {
                sum += a[i][j] < a[id][j];
            }
            if(sum >= 3)
            {
                id = -2;
                break;
            }
        }
        cout << id + 1<< endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes